home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / appe Windows 1.51.1 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-11  |  5.6 KB  |  226 lines  |  [TEXT/KAHL]

  1. // File "main.c" -
  2. // This source file is Copyright Matt Slot & Slot-Machines Ltd., © 1994
  3.  
  4. #include <GestaltEqu.h>
  5. #include <Traps.h>
  6. #include "TextServices.h"
  7.  
  8. #include "main.h"
  9. #include "aevents.h"
  10. #include "contexts.h"
  11. #include "event filter.h"
  12. #include "windows.h"
  13.  
  14. // * ****************************************************************************** *
  15. // Global Vars
  16.  
  17. GlobalsRec glob;
  18.  
  19. // * ****************************************************************************** *
  20. // * ****************************************************************************** *
  21.  
  22. void DoInit() {
  23.     long response;
  24.     
  25.     // NewServiceWindow() needs more stack space... so lets reserve it 
  26.     SetApplLimit(GetApplLimit() - 16384);
  27.     MaxApplZone();
  28.     MoreMasters();
  29.  
  30.     // Basic Initialization 
  31.     // DONT CALL INITWINDOWS() in a background only app
  32.     InitGraf(&thePort);
  33.     
  34.     // Simple Tests for compatibility
  35.     if (Gestalt(gestaltSystemVersion, &response) || (response < 0x0604) ||
  36.             Gestalt(gestaltTSMgrVersion, &response)) {
  37.         NMRecPtr notify;
  38.  
  39.         notify = (NMRecPtr) NewPtrSys(sizeof(*notify));
  40.         notify->qLink = 0;
  41.         notify->qType = nmType;
  42.         notify->nmMark = 0;
  43.         notify->nmIcon = 0;
  44.         notify->nmSound = (Handle) -1;
  45.         notify->nmStr = "\p\"appe Windows\" requires System 7.1 or later";
  46.         notify->nmResp = 0; 
  47.         notify->nmRefCon = 0;    
  48.         NMInstall(notify);
  49.         
  50.         ExitToShell();
  51.         }
  52.  
  53.     // Flags for update routines and for hiding/showing the window
  54.     glob.bkgdOnly = ((** (short **) GetResource('SIZE', -1)) & 0x0400) ? -1 : 0;    
  55.     glob.hasColorQD = (Gestalt(gestaltQuickdrawFeatures, &response) ||
  56.             ((response & (1 << gestaltHasColor)) == 0)) ? 0 : -1;
  57.     glob.hasGDevices = (NGetTrapAddress(_GetDeviceList, ToolTrap) ==
  58.             NGetTrapAddress(_Unimplemented, ToolTrap)) ? 0 : -1;
  59.     glob.hotApplication = kMyApplication;
  60.     glob.frontMBarHeight = 20;    // Until we get somewhere that we can validate this
  61.     
  62.     if (kPatchOutNewWindow) {
  63.         glob.saveNewWindow = NGetTrapAddress(_NewWindow, ToolTrap);
  64.         NSetTrapAddress((long) SmartNewWindow, _NewWindow, ToolTrap);
  65.         }
  66.     
  67.     // Create a temporary resource file
  68.     if (CloneResFile()) ExitToShell();
  69.     
  70.     // Finish our Initialization - but only if we are a foreground app
  71.     // Do this before we do anything else interface related.
  72.     if (! glob.bkgdOnly) {
  73.         InitFonts();
  74.         InitWindows();
  75.         InitMenus();
  76.         TEInit();
  77.         InitDialogs(0);
  78.         
  79.         InsertMenu(GetMenu(kAppleMenuID), 0);
  80.         AddResMenu(GetMHandle(kAppleMenuID), 'DRVR');
  81.         InsertMenu(GetMenu(kFileMenuID), 0);
  82.         InsertMenu(GetMenu(kEditMenuID), 0);
  83.         DisableItem(GetMHandle(kEditMenuID), 0);
  84.         
  85.         DrawMenuBar();
  86.         }
  87.         
  88.     InitHLEvents();
  89.     InsertMyFilter();
  90.     DoCreateWindow();
  91.     }
  92.     
  93. // * ****************************************************************************** *
  94. // * ****************************************************************************** *
  95.  
  96. void DoLoop() {
  97.     
  98.     while(!glob.quitting) {
  99.         if (WaitNextEvent(everyEvent, &glob.theEvent, 60, 0)) {
  100.             switch(glob.theEvent.what) {
  101.                 case mouseDown: {    // Only when in foreground
  102.                     short thePart;
  103.                     WindowPtr whichWin;
  104.                     
  105.                     switch(thePart = FindWindow(glob.theEvent.where, &whichWin)) {
  106.                         case inMenuBar:
  107.                             DoMenuItem(MenuSelect(glob.theEvent.where));
  108.                             break;
  109.                             
  110.                         case inSysWindow:
  111.                             SystemClick(&glob.theEvent, whichWin);
  112.                             break;
  113.                             
  114.                         case inDrag:
  115.                         case inContent:
  116.                         case inGrow:
  117.                         case inGoAway:
  118.                         case inZoomIn:
  119.                         case inZoomOut:
  120.                             break;
  121.                         }
  122.                         
  123.                     break;
  124.                     }
  125.                 case keyDown:
  126.                 case autoKey:  // Only when in foreground
  127.                     if (glob.theEvent.modifiers & cmdKey)
  128.                         DoMenuItem(MenuKey(glob.theEvent.message & charCodeMask));
  129.                     break;
  130.                     
  131.                 case kHighLevelEvent:
  132.                     AEProcessAppleEvent(&glob.theEvent);
  133.                     break;
  134.                 }
  135.             }
  136.         if (! glob.bkgdOnly) {
  137.             SetCursor(&arrow);
  138.             if ((*GetMHandle(kEditMenuID))->enableFlags)
  139.                 DisableItem(GetMHandle(kEditMenuID), 0);
  140.             }
  141.         
  142.         DoCheckVisibility();
  143.         DoIdleWindow();
  144.         }
  145.     
  146.     }
  147.     
  148. // * ****************************************************************************** *
  149. // * ****************************************************************************** *
  150.  
  151. void DoMenuItem(long theMenuAndItem) {
  152.     short theMenu, theItem;
  153.     Str63 theString;
  154.     
  155.     if (! theMenuAndItem) return;
  156.     
  157.     theMenu = (theMenuAndItem & 0xFFFF0000) >> 16;
  158.     theItem = theMenuAndItem & 0x0000FFFF;
  159.     
  160.     switch(theMenu) {
  161.         case kAppleMenuID:
  162.             if (theItem == kAppleMenuAboutItem) {
  163.                 short dlgItem=0;
  164.                 DialogPtr theDlg;
  165.                 // Ewww, it doesnt seem to like Alerts
  166.                 // Alert(128, 0);
  167.                 
  168.                 theDlg = GetNewDialog(128, 0, (WindowPtr) -1);
  169.                 ShowWindow(theDlg);
  170.                 while(!dlgItem) ModalDialog(0, &dlgItem);
  171.                 HideWindow(theDlg);
  172.                 DisposeDialog(theDlg);
  173.                 }
  174.               else {
  175.                 GetItem(GetMHandle(theMenu), theItem, theString);
  176.                 OpenDeskAcc(theString);
  177.                 }
  178.             break;
  179.             
  180.         case kFileMenuID:
  181.             switch(theItem) {
  182.                 case kFileMenuShowHideItem:
  183.                     glob.hidden = (glob.hidden) ? 0 : -1;
  184.                     DoCheckVisibility();
  185.                     break;
  186.  
  187.                 case kFileMenuCloseItem:
  188.                 case kFileMenuQuitItem:
  189.                     glob.quitting = -1;
  190.                     break;
  191.                 }
  192.             break;
  193.             
  194.         case kEditMenuID:
  195.             break;
  196.  
  197.         default:
  198.             break;
  199.         }
  200.     HiliteMenu(0);
  201.     DrawMenuBar();
  202.     }
  203.     
  204. // * ****************************************************************************** *
  205. // * ****************************************************************************** *
  206.  
  207. short DoDispose() {
  208.     
  209.     RemoveMyFilter();
  210.     DoDisposeWindow();    
  211.     FSpDelete(&glob.cloneSpec);
  212.  
  213.     return(0);
  214.     }
  215.     
  216. // * ****************************************************************************** *
  217. // * ****************************************************************************** *
  218.  
  219. void main() {
  220.     DoInit();
  221.  
  222.     do DoLoop();
  223.         while(DoDispose());
  224.     }
  225.  
  226.